package sz.szsmk.citizencard.ui.adapter;

import android.content.Context;
import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.layout.DefaultLayoutHelper;

import java.util.ArrayList;
import java.util.List;

import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.databinding.AppGridViewBinding;
import sz.szsmk.citizencard.model.network.bean.response.QueryAllFunctionNewRes;

/**
 * Copyright (c)2018
 * author: zdh
 * created on: 2018/12/18 17:58
 * description:
 */
public class AppHomeGridAdapter extends DelegateAdapter.Adapter<AppHomeGridAdapter.AppHomeGridHolder> implements View.OnClickListener {

    private Context mContext;
    private DefaultLayoutHelper mGridLayoutHelper;
    private ArrayList<String> mList = new ArrayList<>();
    private ArrayList<Integer> iconList = new ArrayList<>();
    private List<QueryAllFunctionNewRes.ClassListBean.FunctionListBean> viewData;
    private ApphomeRvGridAdapter rvGridAdapter;
    private GridMoreInterface gridMoreInterface;


    private BaseInterface baseInterface;

    public interface BaseInterface {
        public void onClick(View view, String url, String syslabl, int need_header, String id, QueryAllFunctionNewRes.ClassListBean.FunctionListBean bean);
    }

    public void setBaseInterface(BaseInterface baseInterface) {
        this.baseInterface = baseInterface;
    }

    public AppHomeGridAdapter(FragmentActivity activity, DefaultLayoutHelper headHelper) {
        mContext = activity;
        mGridLayoutHelper = headHelper;

    }

    @Override
    public int getItemViewType(int position) {
        return R.layout.app_grid_view;
    }

    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return mGridLayoutHelper;
    }

    @Override
    public AppHomeGridHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return AppHomeGridHolder.creat(parent);
    }

    @Override
    public void onBindViewHolder(AppHomeGridHolder holder, int position) {
        loadItemData(position, holder.binding);
    }

    private void loadItemData(int position, AppGridViewBinding binding) {
        if (viewData != null) {
            RecyclerView appGridRv = binding.appGrid;
            GridLayoutManager manager = new GridLayoutManager(mContext, 5);
            rvGridAdapter = new ApphomeRvGridAdapter(mContext);
            rvGridAdapter.moreOnClick(new ApphomeRvGridAdapter.MoreInterface() {
                @Override
                public void onClick(View view, int position) {
                    if (gridMoreInterface != null) {
                        gridMoreInterface.onClick(view, position);
                    }
                }
            });

            rvGridAdapter.setBaseInterface(new ApphomeRvGridAdapter.BaseInterface() {
                @Override
                public void onClick(View view, String url, String syslabl, int need_header, String id, QueryAllFunctionNewRes.ClassListBean.FunctionListBean bean) {

                    if (baseInterface!=null){
                        baseInterface.onClick(view,url,syslabl,need_header,id,bean);
                    }
                }
            });
            appGridRv.setLayoutManager(manager);
            appGridRv.setAdapter(rvGridAdapter);
            rvGridAdapter.setViewData(viewData);
            rvGridAdapter.notifyDataSetChanged();
        }

    }

    @Override
    public int getItemCount() {
        return 1;
    }

    public void setViewData(List<QueryAllFunctionNewRes.ClassListBean.FunctionListBean> viewData) {
        this.viewData = viewData;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

        }
    }

    public static class AppHomeGridHolder extends RecyclerView.ViewHolder {
        private AppGridViewBinding binding;

        public static AppHomeGridHolder creat(ViewGroup parent) {
            AppGridViewBinding binding =
                    AppGridViewBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);

            return new AppHomeGridHolder(binding);
        }

        public AppHomeGridHolder(AppGridViewBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }

    public interface GridMoreInterface {
        public void onClick(View view, int position);
    }

    public void GridMoreOnClick(GridMoreInterface moreInterface) {
        this.gridMoreInterface = moreInterface;
    }
}
